# Load data
library(weights)
# Set working directory to directory with dataset

load("covidelection.Rda")

# Graphs with respondents' positions
maskspublic <- wpct(dat$maskspublic_you,dat$weights)
closebusiness <- wpct(dat$closebusiness_you,dat$weights)
who <- wpct(dat$who_you,dat$weights)
#0= much more,0.25=somewhat more,0.5=the same as now, 0.75=somewhat less, 1=much less
protectcoronavirus <- wpct(dat$protectcoronavirus,dat$weights)
stayathome <- wpct(dat$stayathome,dat$weights)
coronavirustesting <- wpct(dat$coronavirustesting,dat$weights)

d <- data.frame(y=c(maskspublic,closebusiness,who)*100,issue=c(rep("Masks in Public",3),rep("Close Businesses",3),rep("WHO",3)), answer=c(rep(c("A","D","N"),3)))
d$issue <- factor(d$issue,levels=c("Masks in Public","Close Businesses","WHO"))
d$answer <- factor(d$answer,levels=c("D","N","A"))
pdf("positionsagree.pdf")
ggplot(d,aes(x=answer,y=y)) + geom_col() + facet_grid(~issue) + theme_bw() + ylab("%") + xlab("") + ylim(0,70)
dev.off()

d <- data.frame(y=c(protectcoronavirus,coronavirustesting,stayathome)*100,issue=c(rep("Protect from Coronavirus",5),rep("Coronavirus Testing",5),rep("Stay at Home",5)), answer=c(rep(c("++","+","=","-","--"),3)))
d$issue <- factor(d$issue,levels=c("Coronavirus Testing","Protect from Coronavirus","Stay at Home"))
d$answer <- factor(d$answer,levels=c("--","-","=","+","++"))
pdf("positionseffort.pdf")
ggplot(d,aes(x=answer,y=y)) + geom_col() + facet_grid(~issue) + theme_bw() + ylab("%") + xlab("") + ylim(0,51)
dev.off()

# Among Trump voters

maskspublic_trump <- wpct(dat$maskspublic_you[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
closebusiness_trump <- wpct(dat$closebusiness_you[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
who_trump <- wpct(dat$who_you[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
protectcoronavirus_trump <- wpct(dat$protectcoronavirus[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
stayathome_trump <- wpct(dat$stayathome[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
coronavirustesting_trump <- wpct(dat$coronavirustesting[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

d <- data.frame(y=c(maskspublic_trump,closebusiness_trump,who_trump)*100,issue=c(rep("Masks in Public",3),rep("Close Businesses",3),rep("WHO",3)), answer=c(rep(c("A","D","N"),3)))
d$issue <- factor(d$issue,levels=c("Masks in Public","Close Businesses","WHO"))
d$answer <- factor(d$answer,levels=c("D","N","A"))
pdf("positionsagree_trumpvote.pdf")
ggplot(d,aes(x=answer,y=y)) + geom_col() + facet_grid(~issue) + theme_bw() + ylab("%") + xlab("")  + ylim(0,70)
dev.off()

d <- data.frame(y=c(protectcoronavirus_trump,coronavirustesting_trump,stayathome_trump)*100,issue=c(rep("Protect from Coronavirus",5),rep("Coronavirus Testing",5),rep("Stay at Home",5)), answer=c(rep(c("++","+","=","-","--"),3)))
d$issue <- factor(d$issue,levels=c("Coronavirus Testing","Protect from Coronavirus","Stay at Home"))
d$answer <- factor(d$answer,levels=c("--","-","=","+","++"))
pdf("positionseffort_trumpvote.pdf")
ggplot(d,aes(x=answer,y=y)) + geom_col() + facet_grid(~issue) + theme_bw() + ylab("%") + xlab("")  + ylim(0,51)
dev.off()




# Graph with knowledge of positions on all issues. Appendix A4
improveblacks_biden_bidenv <- wpct(dat$improveblacks_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
improveblacks_trump_bidenv <- wpct(dat$improveblacks_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
improveblacks_biden_trumpv <- wpct(dat$improveblacks_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
improveblacks_trump_trumpv <- wpct(dat$improveblacks_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

improveblacks_cand_bidenv <- wpct(dat$improveblacks_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
improveblacks_cand_trumpv <- wpct(dat$improveblacks_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

ssmban_biden_bidenv <- wpct(dat$ssmban_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
ssmban_trump_bidenv <- wpct(dat$ssmban_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
ssmban_biden_trumpv <- wpct(dat$ssmban_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
ssmban_trump_trumpv <- wpct(dat$ssmban_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

ssmban_cand_bidenv <- wpct(dat$ssmban_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
ssmban_cand_trumpv <- wpct(dat$ssmban_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

lowertax_biden_bidenv <- wpct(dat$lowertax_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
lowertax_trump_bidenv <- wpct(dat$lowertax_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
lowertax_biden_trumpv <- wpct(dat$lowertax_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
lowertax_trump_trumpv <- wpct(dat$lowertax_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

lowertax_cand_bidenv <- wpct(dat$lowertax_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
lowertax_cand_trumpv <- wpct(dat$lowertax_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

abortion_biden_bidenv <- wpct(dat$abortion_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
abortion_trump_bidenv <- wpct(dat$abortion_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
abortion_biden_trumpv <- wpct(dat$abortion_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
abortion_trump_trumpv <- wpct(dat$abortion_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

abortion_cand_bidenv <- wpct(dat$abortion_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
abortion_cand_trumpv <- wpct(dat$abortion_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

backchecks_biden_bidenv <- wpct(dat$backchecks_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
backchecks_trump_bidenv <- wpct(dat$backchecks_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
backchecks_biden_trumpv <- wpct(dat$backchecks_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
backchecks_trump_trumpv <- wpct(dat$backchecks_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

backchecks_cand_bidenv <- wpct(dat$backchecks_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
backchecks_cand_trumpv <- wpct(dat$backchecks_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

co2_biden_bidenv <- wpct(dat$co2_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
co2_trump_bidenv <- wpct(dat$co2_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
co2_biden_trumpv <- wpct(dat$co2_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
co2_trump_trumpv <- wpct(dat$co2_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

co2_cand_bidenv <- wpct(dat$co2_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
co2_cand_trumpv <- wpct(dat$co2_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

muslim_biden_bidenv <- wpct(dat$muslim_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
muslim_trump_bidenv <- wpct(dat$muslim_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
muslim_biden_trumpv <- wpct(dat$muslim_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
muslim_trump_trumpv <- wpct(dat$muslim_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

muslim_cand_bidenv <- wpct(dat$muslim_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
muslim_cand_trumpv <- wpct(dat$muslim_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

repealaca_biden_bidenv <- wpct(dat$repealaca_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
repealaca_trump_bidenv <- wpct(dat$repealaca_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
repealaca_biden_trumpv <- wpct(dat$repealaca_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
repealaca_trump_trumpv <- wpct(dat$repealaca_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

repealaca_cand_bidenv <- wpct(dat$repealaca_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
repealaca_cand_trumpv <- wpct(dat$repealaca_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

who_biden_bidenv <- wpct(dat$who_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
who_trump_bidenv <- wpct(dat$who_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
who_biden_trumpv <- wpct(dat$who_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
who_trump_trumpv <- wpct(dat$who_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

who_cand_bidenv <- wpct(dat$who_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
who_cand_trumpv <- wpct(dat$who_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

mexicowall_biden_bidenv <- wpct(dat$mexicowall_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
mexicowall_trump_bidenv <- wpct(dat$mexicowall_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
mexicowall_biden_trumpv <- wpct(dat$mexicowall_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
mexicowall_trump_trumpv <- wpct(dat$mexicowall_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

mexicowall_cand_bidenv <- wpct(dat$mexicowall_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
mexicowall_cand_trumpv <- wpct(dat$mexicowall_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

maskspublic_biden_bidenv <- wpct(dat$maskspublic_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
maskspublic_trump_bidenv <- wpct(dat$maskspublic_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
maskspublic_biden_trumpv <- wpct(dat$maskspublic_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
maskspublic_trump_trumpv <- wpct(dat$maskspublic_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

maskspublic_cand_bidenv <- wpct(dat$maskspublic_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
maskspublic_cand_trumpv <- wpct(dat$maskspublic_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

freetrade_biden_bidenv <- wpct(dat$freetrade_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
freetrade_trump_bidenv <- wpct(dat$freetrade_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
freetrade_biden_trumpv <- wpct(dat$freetrade_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
freetrade_trump_trumpv <- wpct(dat$freetrade_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

freetrade_cand_bidenv <- wpct(dat$freetrade_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
freetrade_cand_trumpv <- wpct(dat$freetrade_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

incomegap_biden_bidenv <- wpct(dat$incomegap_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
incomegap_trump_bidenv <- wpct(dat$incomegap_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
incomegap_biden_trumpv <- wpct(dat$incomegap_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
incomegap_trump_trumpv <- wpct(dat$incomegap_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

incomegap_cand_bidenv <- wpct(dat$incomegap_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
incomegap_cand_trumpv <- wpct(dat$incomegap_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]

closebusiness_biden_bidenv <- wpct(dat$closebusiness_biden[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
closebusiness_trump_bidenv <- wpct(dat$closebusiness_trump[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])
closebusiness_biden_trumpv <- wpct(dat$closebusiness_biden[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])
closebusiness_trump_trumpv <- wpct(dat$closebusiness_trump[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])

closebusiness_cand_bidenv <- wpct(dat$closebusiness_cand[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"])[2]
closebusiness_cand_trumpv <- wpct(dat$closebusiness_cand[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"])[2]


d <- data.frame(y=c(improveblacks_biden_bidenv,improveblacks_trump_bidenv,improveblacks_biden_trumpv,improveblacks_trump_trumpv,ssmban_biden_bidenv,ssmban_trump_bidenv,ssmban_biden_trumpv,ssmban_trump_trumpv,lowertax_biden_bidenv,lowertax_trump_bidenv,lowertax_biden_trumpv,lowertax_trump_trumpv,abortion_biden_bidenv,abortion_trump_bidenv,abortion_biden_trumpv,abortion_trump_trumpv,backchecks_biden_bidenv,backchecks_trump_bidenv,backchecks_biden_trumpv,backchecks_trump_trumpv,co2_biden_bidenv,co2_trump_bidenv,co2_biden_trumpv,co2_trump_trumpv,muslim_biden_bidenv,muslim_trump_bidenv,muslim_biden_trumpv,muslim_trump_trumpv,repealaca_biden_bidenv,repealaca_trump_bidenv,repealaca_biden_trumpv,repealaca_trump_trumpv,who_biden_bidenv,who_trump_bidenv,who_biden_trumpv,who_trump_trumpv,mexicowall_biden_bidenv,mexicowall_trump_bidenv,mexicowall_biden_trumpv,mexicowall_trump_trumpv,maskspublic_biden_bidenv,maskspublic_trump_bidenv,maskspublic_biden_trumpv,maskspublic_trump_trumpv,freetrade_biden_bidenv,freetrade_trump_bidenv,freetrade_biden_trumpv,freetrade_trump_trumpv,incomegap_biden_bidenv,incomegap_trump_bidenv,incomegap_biden_trumpv,incomegap_trump_trumpv,closebusiness_biden_bidenv,closebusiness_trump_bidenv,closebusiness_biden_trumpv,closebusiness_trump_trumpv)*100,
                correct=c(rep(improveblacks_cand_bidenv,3),rep(improveblacks_cand_bidenv,3),rep(improveblacks_cand_trumpv,3),rep(improveblacks_cand_trumpv,3),rep(ssmban_cand_bidenv,3),rep(ssmban_cand_bidenv,3),rep(ssmban_cand_trumpv,3),rep(ssmban_cand_trumpv,3),rep(lowertax_cand_bidenv,3),rep(lowertax_cand_bidenv,3),rep(lowertax_cand_trumpv,3),rep(lowertax_cand_trumpv,3),rep(abortion_cand_bidenv,3),rep(abortion_cand_bidenv,3),rep(abortion_cand_trumpv,3),rep(abortion_cand_trumpv,3),rep(backchecks_cand_bidenv,3),rep(backchecks_cand_bidenv,3),rep(backchecks_cand_trumpv,3),rep(backchecks_cand_trumpv,3),rep(co2_cand_bidenv,3),rep(co2_cand_bidenv,3),rep(co2_cand_trumpv,3),rep(co2_cand_trumpv,3),rep(muslim_cand_bidenv,3),rep(muslim_cand_bidenv,3),rep(muslim_cand_trumpv,3),rep(muslim_cand_trumpv,3),rep(repealaca_cand_bidenv,3),rep(repealaca_cand_bidenv,3),rep(repealaca_cand_trumpv,3),rep(repealaca_cand_trumpv,3),rep(who_cand_bidenv,3),rep(who_cand_bidenv,3),rep(who_cand_trumpv,3),rep(who_cand_trumpv,3),rep(mexicowall_cand_bidenv,3),rep(mexicowall_cand_bidenv,3),rep(mexicowall_cand_trumpv,3),rep(mexicowall_cand_trumpv,3),rep(maskspublic_cand_bidenv,3),rep(maskspublic_cand_bidenv,3),rep(maskspublic_cand_trumpv,3),rep(maskspublic_cand_trumpv,3),rep(freetrade_cand_bidenv,3),rep(freetrade_cand_bidenv,3),rep(freetrade_cand_trumpv,3),rep(freetrade_cand_trumpv,3),rep(incomegap_cand_bidenv,3),rep(incomegap_cand_bidenv,3),rep(incomegap_cand_trumpv,3),rep(incomegap_cand_trumpv,3),rep(closebusiness_cand_bidenv,3),rep(closebusiness_cand_bidenv,3),rep(closebusiness_cand_trumpv,3),rep(closebusiness_cand_trumpv,3)),
                issue=c(rep("Improve Position of Blacks",12),rep("Gay Marriage Ban",12),rep("Lower Taxes",12), rep("Abortion",12), rep("Background Checks for Guns",12),rep("CO2",12), rep("Muslim Ban",12), rep("Repeal ACA",12), rep("WHO",12), rep("Mexico Wall",12),rep("Masks in Public",12),rep("Free Trade",12),rep("Income Gap",12),rep("Close Businesses",12)),
                candidate=rep(c(rep("Biden",3),rep("Trump",3),rep("Biden",3),rep("Trump",3)),14),
                vote=rep(c(rep("Biden",3),rep("Biden",3),rep("Trump",3),rep("Trump",3)),14),position=c(rep(c("Agree","Disagree","Neither Agree nor Disagree"),56)))
d$position <- factor(d$position,levels=c("Disagree","Neither Agree nor Disagree","Agree"))
d_trump <- subset(d,position=="Agree" & vote=="Trump")
d_trump <- d_trump[order(d_trump$correct,decreasing=T),]
d_trump$issue <- factor(d_trump$issue,levels=unique(d_trump$issue))
d_trump$correct <- as.character(round(100*d_trump$correct))
d_trump$correct <- paste(d_trump$correct,"% Correct")
d_trump$correct[d_trump$candidate=="Trump"] <- NA
pdf("allissueplacements_trumpvoters.pdf")
ggplot(d_trump,aes(position,y,fill=candidate,label=correct)) + geom_col(position="dodge") + theme_bw() + scale_fill_grey(name="Candidate") + xlab("") + ylab("% Believe Candidate Agrees")  + facet_wrap(~issue,nrow=5) + theme(axis.ticks.x = element_blank(),axis.text.x=element_blank()) + geom_text(aes(y=90)) + ylim(0,100)
dev.off()

d_biden <- subset(d,position=="Agree" & vote=="Biden")
d_biden <- d_biden[order(d_biden$correct,decreasing=T),]
d_biden$issue <- factor(d_biden$issue,levels=unique(d_biden$issue))
d_biden$correct <- as.character(round(100*d_biden$correct))
d_biden$correct <- paste(d_biden$correct,"% Correct")
d_biden$correct[d_biden$candidate=="Biden"] <- NA
pdf("allissueplacements_bidenvoters.pdf")
ggplot(d_biden,aes(position,y,fill=candidate,label=correct)) + geom_col(position="dodge") + theme_bw() + scale_fill_grey(name="Candidate") + xlab("") + ylab("% Believe Candidate Agrees")  + facet_wrap(~issue,nrow=5) + theme(axis.ticks.x = element_blank(),axis.text.x=element_blank()) + geom_text(aes(y=90)) + ylim(0,100)
dev.off()


# Perceptions of each candidate's positions
# Recode NAs to skipped
dat$maskspublic_biden_nas <- as.character(dat$maskspublic_biden)
dat$maskspublic_biden_nas[is.na(dat$maskspublic_biden_nas)] <- "Skipped"
dat$maskspublic_biden_nas <- as.factor(dat$maskspublic_biden_nas)
dat$closebusiness_biden_nas <- as.character(dat$closebusiness_biden)
dat$closebusiness_biden_nas[is.na(dat$closebusiness_biden_nas)] <- "Skipped"
dat$closebusiness_biden_nas <- as.factor(dat$closebusiness_biden_nas)
dat$who_biden_nas <- as.character(dat$who_biden)
dat$who_biden_nas[is.na(dat$who_biden_nas)] <- "Skipped"
dat$who_biden_nas <- as.factor(dat$who_biden_nas)
dat$maskspublic_trump_nas <- as.character(dat$maskspublic_trump)
dat$maskspublic_trump_nas[is.na(dat$maskspublic_trump_nas)] <- "Skipped"
dat$maskspublic_trump_nas <- as.factor(dat$maskspublic_trump_nas)
dat$closebusiness_trump_nas <- as.character(dat$closebusiness_trump)
dat$closebusiness_trump_nas[is.na(dat$closebusiness_trump_nas)] <- "Skipped"
dat$closebusiness_trump_nas <- as.factor(dat$closebusiness_trump_nas)
dat$who_trump_nas <- as.character(dat$who_trump)
dat$who_trump_nas[is.na(dat$who_trump_nas)] <- "Skipped"
dat$who_trump_nas <- as.factor(dat$who_trump_nas)

maskspublic <- wpct(dat$maskspublic_biden_nas[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"],na.rm=F)
closebusiness <- wpct(dat$closebusiness_biden_nas[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"],na.rm=F)
who <- wpct(dat$who_biden_nas[dat$vote=="Biden"],dat$weights[dat$vote=="Biden"],na.rm=F)
d <- data.frame(y=c(maskspublic,closebusiness,who)*100,opinion=rep(c("A","N","D","Skipped"),3),issue=c(rep("Masks in Public",4),rep("Close Businesses",4),rep("WHO",4)))
d$opinion <- factor(d$opinion,levels=c("D","N","A","Skipped"))
pdf("perceptions_biden.pdf")
ggplot(d,aes(opinion,y)) + geom_col() + facet_wrap(~issue) + theme_bw() + ylab("% Who Think Biden...") + xlab("") + ylim(0,82)
dev.off()

maskspublic <- wpct(dat$maskspublic_trump_nas[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"],na.rm=T)
closebusiness <- wpct(dat$closebusiness_trump_nas[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"],na.rm=F)
who <- wpct(dat$who_trump_nas[dat$vote=="Trump"],dat$weights[dat$vote=="Trump"],na.rm=F)
d <- data.frame(y=c(maskspublic,closebusiness,who)*100,opinion=rep(c("A","D","N","Skipped"),3),issue=c(rep("Masks in Public",4),rep("Close Businesses",4),rep("WHO",4)))
d$opinion <- factor(d$opinion,levels=c("D","N","A","Skipped"))
pdf("perceptions_trump.pdf")
ggplot(d,aes(opinion,y)) + geom_col() + facet_wrap(~issue) + theme_bw() + ylab("% Who Think Trump...") + xlab("") + ylim(0,82)
dev.off()

# Perceptions by respondent's opinion
maskspublic <- c(wpct(dat$maskspublic_biden[dat$vote=="Biden" & dat$maskspublic_you=="Agree"],dat$weights[dat$vote=="Biden" & dat$maskspublic_you=="Agree"])[1],wpct(dat$maskspublic_biden[dat$vote=="Biden" & dat$maskspublic_you=="Neither agree nor disagree"],dat$weights[dat$vote=="Biden" & dat$maskspublic_you=="Neither agree nor disagree"])[1],wpct(dat$maskspublic_biden[dat$vote=="Biden" & dat$maskspublic_you=="Disagree"],dat$weights[dat$vote=="Biden" & dat$maskspublic_you=="Disagree"])[1])
closebusiness <- c(wpct(dat$closebusiness_biden[dat$vote=="Biden" & dat$closebusiness_you=="Agree"],dat$weights[dat$vote=="Biden" & dat$closebusiness_you=="Agree"])[1],wpct(dat$closebusiness_biden[dat$vote=="Biden" & dat$closebusiness_you=="Neither agree nor disagree"],dat$weights[dat$vote=="Biden" & dat$closebusiness_you=="Neither agree nor disagree"])[1],wpct(dat$closebusiness_biden[dat$vote=="Biden" & dat$closebusiness_you=="Disagree"],dat$weights[dat$vote=="Biden" & dat$closebusiness_you=="Disagree"])[1])
who <- c(wpct(dat$who_biden[dat$vote=="Biden" & dat$who_you=="Agree"],dat$weights[dat$vote=="Biden" & dat$who_you=="Agree"])[1],wpct(dat$who_biden[dat$vote=="Biden" & dat$who_you=="Neither agree nor disagree"],dat$weights[dat$vote=="Biden" & dat$who_you=="Neither agree nor disagree"])[1],wpct(dat$who_biden[dat$vote=="Biden" & dat$who_you=="Disagree"],dat$weights[dat$vote=="Biden" & dat$who_you=="Disagree"])[1])
d <- data.frame(y=c(maskspublic,closebusiness,who)*100,opinion=rep(c("A","N","D"),3),issue=c(rep("Masks in Public",3),rep("Close Businesses",3),rep("WHO",3)))
d$opinion <- factor(d$opinion,levels=c("D","N","A"))
pdf("perceptionsbyopinion_biden.pdf")
ggplot(d,aes(opinion,y)) + geom_col() + facet_wrap(~issue) + theme_bw() + ylab("% Who Think Biden Agrees") + xlab("") + ylim(0,95)
dev.off()

maskspublic <- c(wpct(dat$maskspublic_trump[dat$vote=="Trump" & dat$maskspublic_you=="Agree"],dat$weights[dat$vote=="Trump" & dat$maskspublic_you=="Agree"])[1],wpct(dat$maskspublic_trump[dat$vote=="Trump" & dat$maskspublic_you=="Neither agree nor disagree"],dat$weights[dat$vote=="Trump" & dat$maskspublic_you=="Neither agree nor disagree"])[1],wpct(dat$maskspublic_trump[dat$vote=="Trump" & dat$maskspublic_you=="Disagree"],dat$weights[dat$vote=="Trump" & dat$maskspublic_you=="Disagree"])[1])
closebusiness <- c(wpct(dat$closebusiness_trump[dat$vote=="Trump" & dat$closebusiness_you=="Agree"],dat$weights[dat$vote=="Trump" & dat$closebusiness_you=="Agree"])[1],wpct(dat$closebusiness_trump[dat$vote=="Trump" & dat$closebusiness_you=="Neither agree nor disagree"],dat$weights[dat$vote=="Trump" & dat$closebusiness_you=="Neither agree nor disagree"])[1],wpct(dat$closebusiness_trump[dat$vote=="Trump" & dat$closebusiness_you=="Disagree"],dat$weights[dat$vote=="Trump" & dat$closebusiness_you=="Disagree"])[1])
who <- c(wpct(dat$who_trump[dat$vote=="Trump" & dat$who_you=="Agree"],dat$weights[dat$vote=="Trump" & dat$who_you=="Agree"])[1],wpct(dat$who_trump[dat$vote=="Trump" & dat$who_you=="Neither agree nor disagree"],dat$weights[dat$vote=="Trump" & dat$who_you=="Neither agree nor disagree"])[1],wpct(dat$who_trump[dat$vote=="Trump" & dat$who_you=="Disagree"],dat$weights[dat$vote=="Trump" & dat$who_you=="Disagree"])[1])
d <- data.frame(y=c(maskspublic,closebusiness,who)*100,opinion=rep(c("A","N","D"),3),issue=c(rep("Masks in Public",3),rep("Close Businesses",3),rep("WHO",3)))
d$opinion <- factor(d$opinion,levels=c("D","N","A"))
pdf("perceptionsbyopinion_trump.pdf")
ggplot(d,aes(opinion,y)) + geom_col() + facet_wrap(~issue) + theme_bw() + ylab("% Who Think Trump Agrees") + xlab("") + ylim(0,95)
dev.off()



# Does importance improve placement knowledge?
wpct(dat$maskspublic_cand[dat$coronavirus_died==1],dat$weights[dat$coronavirus_died==1])
wpct(dat$maskspublic_cand[dat$coronavirus_died==0],dat$weights[dat$coronavirus_died==0])

wpct(dat$closebusiness_cand[dat$coronavirus_died==1],dat$weights[dat$coronavirus_died==1])
wpct(dat$closebusiness_cand[dat$coronavirus_died==0],dat$weights[dat$coronavirus_died==0])

wpct(dat$who_cand[dat$coronavirus_died==1],dat$weights[dat$coronavirus_died==1])
wpct(dat$who_cand[dat$coronavirus_died==0],dat$weights[dat$coronavirus_died==0])

wpct(dat$maskspublic_cand[dat$coronavirus_test==1],dat$weights[dat$coronavirus_test==1])
wpct(dat$maskspublic_cand[dat$coronavirus_test==0],dat$weights[dat$coronavirus_test==0])

wpct(dat$closebusiness_cand[dat$coronavirus_test==1],dat$weights[dat$coronavirus_test==1])
wpct(dat$closebusiness_cand[dat$coronavirus_test==0],dat$weights[dat$coronavirus_test==0])

wpct(dat$who_cand[dat$coronavirus_test==1],dat$weights[dat$coronavirus_test==1])
wpct(dat$who_cand[dat$coronavirus_test==0],dat$weights[dat$coronavirus_test==0])

# Errors people make
wpct(dat$maskspublic_trump[dat$maskspublic_cand==0]=="Agree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Agree",dat$weights[dat$maskspublic_cand==0])
wpct(dat$maskspublic_trump[dat$maskspublic_cand==0]=="Disagree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Disagree",dat$weights[dat$maskspublic_cand==0])
wpct(dat$maskspublic_trump[dat$maskspublic_cand==0]=="Neither agree nor disagree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Neither agree nor disagree",dat$weights[dat$maskspublic_cand==0])
wpct(dat$maskspublic_trump[dat$maskspublic_cand==0]=="Agree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Disagree",dat$weights[dat$maskspublic_cand==0])
wpct(dat$maskspublic_trump[dat$maskspublic_cand==0]=="Neither agree nor disagree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Disagree",dat$weights[dat$maskspublic_cand==0])
wpct(dat$maskspublic_trump[dat$maskspublic_cand==0]=="Agree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Neither agree nor disagree",dat$weights[dat$maskspublic_cand==0])
# Both errors where place Trump as more supportive
wpct(dat$maskspublic_trump[dat$maskspublic_cand==0]=="Agree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Neither agree nor disagree" | dat$maskspublic_trump[dat$maskspublic_cand==0]=="Neither agree nor disagree" & dat$maskspublic_biden[dat$maskspublic_cand==0]=="Disagree",dat$weights[dat$maskspublic_cand==0])


# Analyses of vote choice
dat$votetrump <- dat$vote
levels(dat$votetrump) <- c(NA,1,0,NA,NA)
dat$votetrump <- as.numeric(as.character(dat$votetrump))

wpct(dat$coronavirus>0.5,dat$weights,na.rm=T)
wpct(dat$votetrump[dat$coronavirus>0.5],dat$weights[dat$coronavirus>0.5],na.rm=T)
wpct(dat$votetrump[dat$coronavirus>quantile(dat$coronavirus,prob=0.50,na.rm=T)],dat$weights[dat$coronavirus>quantile(dat$coronavirus,prob=0.50,na.rm=T)],na.rm=T)
wpct(dat$votetrump[dat$coronavirus>quantile(dat$coronavirus,prob=0.75,na.rm=T)],dat$weights[dat$coronavirus>quantile(dat$coronavirus,prob=0.75,na.rm=T)],na.rm=T)

# Reverse coronavirus scale so higher values represent more protective policies
dat$coronavirus <- 1 - dat$coronavirus
pdf("covidvote.pdf")
ggplot(dat,aes(coronavirus,votetrump)) + geom_jitter(height=0.05,width=0.05,size=1) + geom_smooth(se=F,type="loess",colour="grey") + ylab("Probability Trump Vote") + xlab("Six-Item Coronavirus Scale") + theme_bw() + scale_colour_grey(start=0.9,end=0.1,name="Tested Positive") + theme(legend.position = c(0.45, .4), legend.justification = c("right", "top")) + theme(legend.background=element_blank()) + ylim(0,1) + scale_y_continuous(breaks=c(0,0.25,0.5,0.75,1))
dev.off()

# Vote choice by covid attitude separately for each importance measure
levels(dat$coronavirus_test) <- c("No","Yes")
pdf("covidvotebytest.pdf")
ggplot(dat[!is.na(dat$coronavirus_test),],aes(coronavirus,votetrump,color=coronavirus_test)) + geom_jitter(height=0.05,width=0.05,size=1) + geom_smooth(se=F,type="loess") + ylab("Probability Trump Vote") + xlab("Six-Item Coronavirus Scale") + theme_bw() + scale_colour_grey(start=0.9,end=0.1,name="Tested Positive") + theme(legend.position = c(0.45, .4), legend.justification = c("right", "top")) + theme(legend.background=element_blank()) + ylim(0,1) + scale_y_continuous(breaks=c(0,0.25,0.5,0.75,1))
dev.off()

levels(dat$coronavirus_died) <- c("No","Yes")
pdf("covidvotebydied.pdf")
ggplot(dat[!is.na(dat$coronavirus_died),],aes(coronavirus,votetrump,color=coronavirus_died)) + geom_jitter(height=0.05,width=0.05,size=1) + geom_smooth(se=F,type="loess") + ylab("Probability Trump Vote") + xlab("Six-Item Coronavirus Scale") + theme_bw() + scale_colour_grey(start=0.9,end=0.1,name="Died from COVID") + theme(legend.position = c(0.45, .4), legend.justification = c("right", "bottom")) + theme(legend.background=element_blank()) + ylim(0,1) + scale_y_continuous(breaks=c(0,0.25,0.5,0.75,1))
dev.off()


# Scale of correct coronavirus placements
dat$coronaviruspos_cand <- dat$maskspublic_cand + dat$closebusiness_cand + dat$who_cand
dat$coronaviruspos_cand <- as.factor(ifelse(dat$coronaviruspos_cand>median(dat$coronaviruspos_cand,na.rm=T),1,0))
levels(dat$coronaviruspos_cand) <- c("Low","High")

pdf("covidvotebyknowledge.pdf")
ggplot(dat,aes(coronavirus,votetrump,color=coronaviruspos_cand)) + geom_jitter(height=0.05,width=0.05,size=1) + geom_smooth(se=F,type="loess") + ylab("Probability Trump Vote") + xlab("Six-Item Coronavirus Scale") + theme_bw() + scale_colour_grey(start=0.9,end=0.1,name="Knowledge of Candidate Stances") + theme(legend.position = c(0.45, .4), legend.justification = c("right", "top")) + theme(legend.background=element_blank()) + ylim(0,1) + scale_y_continuous(breaks=c(0,0.25,0.5,0.75,1))
dev.off()

wpct(dat$votetrump[dat$coronavirus>median(dat$coronavirus,na.rm=T) & dat$coronaviruspos_cand=="Low"],dat$weights[dat$coronavirus>median(dat$coronavirus,na.rm=T) & dat$coronaviruspos_cand=="Low"],na.rm=T)
wpct(dat$votetrump[dat$coronavirus<median(dat$coronavirus,na.rm=T) & dat$coronaviruspos_cand=="Low"],dat$weights[dat$coronavirus<median(dat$coronavirus,na.rm=T) & dat$coronaviruspos_cand=="Low"],na.rm=T)



